
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
  <head>
    <meta charset="utf-8" />
    <title>http.client --- HTTP 协议客户端 &#8212; Python 3.7.8 文档</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    <script type="text/javascript" src="../_static/translations.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="在 Python 3.7.8 文档 中搜索"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="版权所有" href="../copyright.html" />
    <link rel="next" title="ftplib --- FTP 协议客户端" href="ftplib.html" />
    <link rel="prev" title="http --- HTTP 模块" href="http.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/3/library/http.client.html" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
    
    
    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>
 

  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="ftplib.html" title="ftplib --- FTP 协议客户端"
             accesskey="N">下一页</a> |</li>
        <li class="right" >
          <a href="http.html" title="http --- HTTP 模块"
             accesskey="P">上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">互联网协议和支持</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-http.client">
<span id="http-client-http-protocol-client"></span><h1><a class="reference internal" href="#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a> --- HTTP 协议客户端<a class="headerlink" href="#module-http.client" title="永久链接至标题">¶</a></h1>
<p><strong>源代码：</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.7/Lib/http/client.py">Lib/http/client.py</a></p>
<span class="target" id="index-0"></span><hr class="docutils" id="index-1" />
<p>这个模块定义了实现 HTTP 和 HTTPS 协议客户端的类。 它通常不直接使用 --- 模块 <a class="reference internal" href="urllib.request.html#module-urllib.request" title="urllib.request: Extensible library for opening URLs."><code class="xref py py-mod docutils literal notranslate"><span class="pre">urllib.request</span></code></a> 用它来处理使用 HTTP 和 HTTPS 的 URL。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p>The <a class="reference external" href="https://requests.readthedocs.io/en/master/">Requests package</a>
is recommended for a higher-level HTTP client interface.</p>
</div>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>HTTPS 支持仅在编译 Python 时启用了 SSL 支持的情况下（通过 <a class="reference internal" href="ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> 模块）可用。</p>
</div>
<p>该模块支持以下类：</p>
<dl class="class">
<dt id="http.client.HTTPConnection">
<em class="property">class </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPConnection</code><span class="sig-paren">(</span><em class="sig-param">host</em>, <em class="sig-param">port=None</em>, <span class="optional">[</span><em class="sig-param">timeout</em>, <span class="optional">]</span><em class="sig-param">source_address=None</em>, <em class="sig-param">blocksize=8192</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> 的实例代表与 HTTP 的一个连接事务。 它的实例化应当传入一个主机和可选的端口号。 如果没有传入端口号，如果主机字符串的形式为 <code class="docutils literal notranslate"><span class="pre">主机:端口</span></code> 则会从中提取端口，否则将使用默认的 HTTP 端口（80）。 如果给出了可选的 <em>timeout</em> 参数，则阻塞操作（例如连接尝试）将在指定的秒数之后超时（如果未给出，则使用全局默认超时设置）。 可选的 <em>source_address</em> 参数可以为一个 (主机, 端口) 元组，用作进行 HTTP 连接的源地址。 可选的 <em>blocksize</em> 参数可以字节为单位设置缓冲区的大小，用来发送文件类消息体。</p>
<p>举个例子，以下调用都是创建连接到同一主机和端口的服务器的实例：</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">h1</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h2</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org:80&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h3</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">,</span> <span class="mi">80</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">h4</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s1">&#39;www.python.org&#39;</span><span class="p">,</span> <span class="mi">80</span><span class="p">,</span> <span class="n">timeout</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了*source_address* 参数</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>删除了 <em>strict</em> 参数，不再支持 HTTP 0.9 风格的“简单响应”。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7 版更改: </span>添加了 <em>blocksize</em> 参数。</p>
</div>
</dd></dl>

<dl class="class">
<dt id="http.client.HTTPSConnection">
<em class="property">class </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPSConnection</code><span class="sig-paren">(</span><em class="sig-param">host</em>, <em class="sig-param">port=None</em>, <em class="sig-param">key_file=None</em>, <em class="sig-param">cert_file=None</em>, <span class="optional">[</span><em class="sig-param">timeout</em>, <span class="optional">]</span><em class="sig-param">source_address=None</em>, <em class="sig-param">*</em>, <em class="sig-param">context=None</em>, <em class="sig-param">check_hostname=None</em>, <em class="sig-param">blocksize=8192</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPSConnection" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> 的子类，使用 SSL 与安全服务器进行通信。 默认端口为 <code class="docutils literal notranslate"><span class="pre">443</span></code>。 如果指定了 <em>context</em>，它必须为一个描述 SSL 各选项的 <a class="reference internal" href="ssl.html#ssl.SSLContext" title="ssl.SSLContext"><code class="xref py py-class docutils literal notranslate"><span class="pre">ssl.SSLContext</span></code></a> 实例。</p>
<p>请参阅 <a class="reference internal" href="ssl.html#ssl-security"><span class="std std-ref">Security considerations</span></a> 了解有关最佳实践的更多信息。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>添加了 <em>source_address</em>, <em>context</em> 和 <em>check_hostname</em>。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.2 版更改: </span>这个类目前会在可能的情况下（即如果 <a class="reference internal" href="ssl.html#ssl.HAS_SNI" title="ssl.HAS_SNI"><code class="xref py py-data docutils literal notranslate"><span class="pre">ssl.HAS_SNI</span></code></a> 为真值）支持 HTTPS 虚拟主机。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>删除了 <em>strict</em> 参数，不再支持 HTTP 0.9 风格的“简单响应”。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4.3 版更改: </span>目前这个类在默认情况下会执行所有必要的证书和主机检查。 要回复到先前的非验证行为，可以将 <code class="xref py py-func docutils literal notranslate"><span class="pre">ssl._create_unverified_context()</span></code> 传递给 <em>context</em> 参数。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.7.4 版更改: </span>该类现在对于默认的 <em>context</em> 或在传入 <em>cert_file</em> 并附带自定义 <em>context</em> 时会启用 TLS 1.3 <a class="reference internal" href="ssl.html#ssl.SSLContext.post_handshake_auth" title="ssl.SSLContext.post_handshake_auth"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.post_handshake_auth</span></code></a>。</p>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.6 版后已移除: </span><em>key_file</em> 和 <em>cert_file</em> 已弃用并转而推荐 <em>context</em>。 请改用 <a class="reference internal" href="ssl.html#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.load_cert_chain()</span></code></a> 或让 <a class="reference internal" href="ssl.html#ssl.create_default_context" title="ssl.create_default_context"><code class="xref py py-func docutils literal notranslate"><span class="pre">ssl.create_default_context()</span></code></a> 为你选择系统所信任的 CA 证书。</p>
<p><em>check_hostname</em> 参数也已弃用；应当改用 <em>context</em> 的 <a class="reference internal" href="ssl.html#ssl.SSLContext.check_hostname" title="ssl.SSLContext.check_hostname"><code class="xref py py-attr docutils literal notranslate"><span class="pre">ssl.SSLContext.check_hostname</span></code></a> 属性。</p>
</div>
</dd></dl>

<dl class="class">
<dt id="http.client.HTTPResponse">
<em class="property">class </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPResponse</code><span class="sig-paren">(</span><em class="sig-param">sock</em>, <em class="sig-param">debuglevel=0</em>, <em class="sig-param">method=None</em>, <em class="sig-param">url=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse" title="永久链接至目标">¶</a></dt>
<dd><p>在成功连接后返回类的实例，而不是由用户直接实例化。</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.4 版更改: </span>删除了 <em>strict</em> 参数，不再支持HTTP 0.9 风格的“简单响应”。</p>
</div>
</dd></dl>

<p>下列异常可以适当地被引发:</p>
<dl class="exception">
<dt id="http.client.HTTPException">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPException</code><a class="headerlink" href="#http.client.HTTPException" title="永久链接至目标">¶</a></dt>
<dd><p>此模块中其他异常的基类。 它是 <a class="reference internal" href="exceptions.html#Exception" title="Exception"><code class="xref py py-exc docutils literal notranslate"><span class="pre">Exception</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.NotConnected">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">NotConnected</code><a class="headerlink" href="#http.client.NotConnected" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.InvalidURL">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">InvalidURL</code><a class="headerlink" href="#http.client.InvalidURL" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类，如果给出了一个非数字或为空值的端口就会被引发。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.UnknownProtocol">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">UnknownProtocol</code><a class="headerlink" href="#http.client.UnknownProtocol" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.UnknownTransferEncoding">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">UnknownTransferEncoding</code><a class="headerlink" href="#http.client.UnknownTransferEncoding" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.UnimplementedFileMode">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">UnimplementedFileMode</code><a class="headerlink" href="#http.client.UnimplementedFileMode" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.IncompleteRead">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">IncompleteRead</code><a class="headerlink" href="#http.client.IncompleteRead" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.ImproperConnectionState">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">ImproperConnectionState</code><a class="headerlink" href="#http.client.ImproperConnectionState" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.CannotSendRequest">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">CannotSendRequest</code><a class="headerlink" href="#http.client.CannotSendRequest" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.ImproperConnectionState" title="http.client.ImproperConnectionState"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImproperConnectionState</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.CannotSendHeader">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">CannotSendHeader</code><a class="headerlink" href="#http.client.CannotSendHeader" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.ImproperConnectionState" title="http.client.ImproperConnectionState"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImproperConnectionState</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.ResponseNotReady">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">ResponseNotReady</code><a class="headerlink" href="#http.client.ResponseNotReady" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.ImproperConnectionState" title="http.client.ImproperConnectionState"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ImproperConnectionState</span></code></a> 的一个子类。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.BadStatusLine">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">BadStatusLine</code><a class="headerlink" href="#http.client.BadStatusLine" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。 如果服务器反馈了一个我们不理解的 HTTP 状态码就会被引发。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.LineTooLong">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">LineTooLong</code><a class="headerlink" href="#http.client.LineTooLong" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="#http.client.HTTPException" title="http.client.HTTPException"><code class="xref py py-exc docutils literal notranslate"><span class="pre">HTTPException</span></code></a> 的一个子类。 如果在 HTTP 协议中从服务器接收到过长的行就会被引发。</p>
</dd></dl>

<dl class="exception">
<dt id="http.client.RemoteDisconnected">
<em class="property">exception </em><code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">RemoteDisconnected</code><a class="headerlink" href="#http.client.RemoteDisconnected" title="永久链接至目标">¶</a></dt>
<dd><p><a class="reference internal" href="exceptions.html#ConnectionResetError" title="ConnectionResetError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionResetError</span></code></a> 和 <a class="reference internal" href="#http.client.BadStatusLine" title="http.client.BadStatusLine"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadStatusLine</span></code></a> 的一个子类。 当尝试读取响应时的结果是未从连接读取到数据时由 <a class="reference internal" href="#http.client.HTTPConnection.getresponse" title="http.client.HTTPConnection.getresponse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">HTTPConnection.getresponse()</span></code></a> 引发，表明远端已关闭连接。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.5 新版功能: </span>在此之前引发的异常为 <a class="reference internal" href="#http.client.BadStatusLine" title="http.client.BadStatusLine"><code class="xref py py-exc docutils literal notranslate"><span class="pre">BadStatusLine</span></code></a><code class="docutils literal notranslate"><span class="pre">('')</span></code>。</p>
</div>
</dd></dl>

<p>此模块中定义的常量为：</p>
<dl class="data">
<dt id="http.client.HTTP_PORT">
<code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTP_PORT</code><a class="headerlink" href="#http.client.HTTP_PORT" title="永久链接至目标">¶</a></dt>
<dd><p>HTTP 协议默认的端口号 (总是 <code class="docutils literal notranslate"><span class="pre">80</span></code>)。</p>
</dd></dl>

<dl class="data">
<dt id="http.client.HTTPS_PORT">
<code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">HTTPS_PORT</code><a class="headerlink" href="#http.client.HTTPS_PORT" title="永久链接至目标">¶</a></dt>
<dd><p>HTTPS 协议默认的端口号 (总是 <code class="docutils literal notranslate"><span class="pre">443</span></code>)。</p>
</dd></dl>

<dl class="data">
<dt id="http.client.responses">
<code class="sig-prename descclassname">http.client.</code><code class="sig-name descname">responses</code><a class="headerlink" href="#http.client.responses" title="永久链接至目标">¶</a></dt>
<dd><p>这个字典把 HTTP 1.1 状态码映射到 W3C 名称。</p>
<p>例如：<code class="docutils literal notranslate"><span class="pre">http.client.responses[http.client.NOT_FOUND]</span></code> 是 <code class="docutils literal notranslate"><span class="pre">'NOT</span> <span class="pre">FOUND</span></code> （未发现）。</p>
</dd></dl>

<p>本模块中可用的 HTTP 状态码常量可以参见 <a class="reference internal" href="http.html#http-status-codes"><span class="std std-ref">HTTP 状态码</span></a> 。</p>
<div class="section" id="httpconnection-objects">
<span id="id1"></span><h2>HTTPConnection 对象<a class="headerlink" href="#httpconnection-objects" title="永久链接至标题">¶</a></h2>
<p><a class="reference internal" href="#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> 实例拥有以下方法：</p>
<dl class="method">
<dt id="http.client.HTTPConnection.request">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">request</code><span class="sig-paren">(</span><em class="sig-param">method</em>, <em class="sig-param">url</em>, <em class="sig-param">body=None</em>, <em class="sig-param">headers={}</em>, <em class="sig-param">*</em>, <em class="sig-param">encode_chunked=False</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.request" title="永久链接至目标">¶</a></dt>
<dd><p>这会使用 HTTP 请求方法 <em>method</em> 和选择器 <em>url</em> 向服务器发送请求。</p>
<p>如果给定 <em>body</em>，那么给定的数据会在信息头完成之后发送。它可能是一个 <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 、一个 <a class="reference internal" href="../glossary.html#term-bytes-like-object"><span class="xref std std-term">bytes-like object</span></a> 、一个打开的 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>，或者 <a class="reference internal" href="stdtypes.html#bytes" title="bytes"><code class="xref py py-class docutils literal notranslate"><span class="pre">bytes</span></code></a> 迭代器。如果 <em>body</em> 是字符串，它会按 HTTP 默认的 ISO-8859-1 编码；如果是一个字节类对象，它会按原样发送；如果是 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a> ，文件的内容会被发送，这个文件对象应该支持 <code class="docutils literal notranslate"><span class="pre">read()</span></code> 方法。如果这个文件对象是一个 <a class="reference internal" href="io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOBase</span></code></a> 实例， <code class="docutils literal notranslate"><span class="pre">read()</span></code> 方法返回的数据会按 ISO-8859-1 编码，否则 <code class="docutils literal notranslate"><span class="pre">read()</span></code> 方法返回的数据会按原样发送；如果 <em>body</em> 是一个迭代器，迭代器中的元素会被发送，直到迭代器耗尽。</p>
<p><em>headers</em> 参数应是额外的随请求发送的 HTTP 信息头的字典。</p>
<p>如果 <em>headers</em> 既不包含 Content-Length 也没有 Transfer-Encoding，但存在请求正文，那么这些头字段中的一个会自动设定。如果 <em>body</em> 是 <code class="docutils literal notranslate"><span class="pre">None</span></code>，那么对于要求正文的方法 (<code class="docutils literal notranslate"><span class="pre">PUT</span></code>，<code class="docutils literal notranslate"><span class="pre">POST</span></code>，和 <code class="docutils literal notranslate"><span class="pre">PATCH</span></code>)，Content-Length 头会被设为 <code class="docutils literal notranslate"><span class="pre">0</span></code>。如果 <em>body</em> 是字符串或者类似字节的对象，并且也不是 <a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">文件</span></a>，Content-Length 头会设为正文的长度。任何其他类型的 <em>body</em> （一般是文件或迭代器）会按块编码，这时会自动设定 Transfer-Encoding 头以代替 Content-Length。</p>
<p>在 <em>headers</em> 中指定 Transfer-Encoding 时， <em>encode_chunked</em> 是唯一相关的参数。如果 <em>encode_chunked</em> 为 <code class="docutils literal notranslate"><span class="pre">False</span></code>，HTTPConnection 对象会假定所有的编码都由调用代码处理。如果为 <code class="docutils literal notranslate"><span class="pre">True</span></code>，正文会按块编码。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>HTTP 协议在 1.1 版中添加了块传输编码。除非明确知道 HTTP 服务器可以处理 HTTP 1.1，调用者要么必须指定 Content-Length，要么必须传入 <a class="reference internal" href="stdtypes.html#str" title="str"><code class="xref py py-class docutils literal notranslate"><span class="pre">str</span></code></a> 或字节类对象，注意该对象不能是表达 body 的文件。</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能: </span><em>body</em> 现在可以是可迭代对象了。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.6 版更改: </span>如果 Content-Length 和 Transfer-Encoding 都没有在 <em>headers</em> 中设置，文件和可迭代的 <em>body</em> 对象现在会按块编码。添加了 <em>encode_chunked</em> 参数。不会尝试去确定文件对象的 Content-Length。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPConnection.getresponse">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">getresponse</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.getresponse" title="永久链接至目标">¶</a></dt>
<dd><p>应当在发送一个请求从服务器获取响应时被调用。 返回一个 <a class="reference internal" href="#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code></a> 的实例。</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>请注意你必须在读取了整个响应之后才能向服务器发送新的请求。</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>如果引发了 <a class="reference internal" href="exceptions.html#ConnectionError" title="ConnectionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">ConnectionError</span></code></a> 或其子类， <a class="reference internal" href="#http.client.HTTPConnection" title="http.client.HTTPConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPConnection</span></code></a> 对象将在发送新的请求时准备好重新连接。</p>
</div>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPConnection.set_debuglevel">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">set_debuglevel</code><span class="sig-paren">(</span><em class="sig-param">level</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.set_debuglevel" title="永久链接至目标">¶</a></dt>
<dd><p>设置调试等级。 默认的调试等级为 <code class="docutils literal notranslate"><span class="pre">0</span></code>，意味着不会打印调试输出。 任何大于 <code class="docutils literal notranslate"><span class="pre">0</span></code> 的值将使得所有当前定义的调试输出被打印到 stdout。 <code class="docutils literal notranslate"><span class="pre">debuglevel</span></code> 会被传给任何新创建的 <a class="reference internal" href="#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code></a> 对象。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.1 新版功能.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPConnection.set_tunnel">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">set_tunnel</code><span class="sig-paren">(</span><em class="sig-param">host</em>, <em class="sig-param">port=None</em>, <em class="sig-param">headers=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.set_tunnel" title="永久链接至目标">¶</a></dt>
<dd><p>为 HTTP 连接隧道设置主机和端口。 这将允许通过代理服务器运行连接。</p>
<p>host 和 port 参数指明隧道连接的位置（即 CONNECT 请求所包含的地址，而 <em>不是</em> 代理服务器的地址）。</p>
<p>headers 参数应为一个随 CONNECT 请求发送的额外 HTTP 标头的映射。</p>
<p>例如，要通过一个运行于本机 8080 端口的 HTTPS 代理服务器隧道，我们应当向 <a class="reference internal" href="#http.client.HTTPSConnection" title="http.client.HTTPSConnection"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSConnection</span></code></a> 构造器传入代理的地址，并将我们最终想要访问的主机地址传给 <a class="reference internal" href="#http.client.HTTPConnection.set_tunnel" title="http.client.HTTPConnection.set_tunnel"><code class="xref py py-meth docutils literal notranslate"><span class="pre">set_tunnel()</span></code></a> 方法:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="s2">&quot;localhost&quot;</span><span class="p">,</span> <span class="mi">8080</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">set_tunnel</span><span class="p">(</span><span class="s2">&quot;www.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;HEAD&quot;</span><span class="p">,</span><span class="s2">&quot;/index.html&quot;</span><span class="p">)</span>
</pre></div>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.2 新版功能.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPConnection.connect">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">connect</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.connect" title="永久链接至目标">¶</a></dt>
<dd><p>当对象被创建后连接到指定的服务器。 默认情况下，如果客户端还未建立连接，此函数会在发送请求时自动被调用。</p>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPConnection.close">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">close</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.close" title="永久链接至目标">¶</a></dt>
<dd><p>关闭到服务器的连接。</p>
</dd></dl>

<dl class="attribute">
<dt id="http.client.HTTPConnection.blocksize">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">blocksize</code><a class="headerlink" href="#http.client.HTTPConnection.blocksize" title="永久链接至目标">¶</a></dt>
<dd><p>用于发送文件类消息体的缓冲区大小。</p>
<div class="versionadded">
<p><span class="versionmodified added">3.7 新版功能.</span></p>
</div>
</dd></dl>

<p>作为对使用上述 <code class="xref py py-meth docutils literal notranslate"><span class="pre">request()</span></code> 方法的替代同，你也可以通过使用下面的四个函数，分步骤发送请的请求。</p>
<dl class="method">
<dt id="http.client.HTTPConnection.putrequest">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">putrequest</code><span class="sig-paren">(</span><em class="sig-param">method</em>, <em class="sig-param">url</em>, <em class="sig-param">skip_host=False</em>, <em class="sig-param">skip_accept_encoding=False</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.putrequest" title="永久链接至目标">¶</a></dt>
<dd><p>This should be the first call after the connection to the server has been
made. It sends a line to the server consisting of the <em>method</em> string,
the <em>url</em> string, and the HTTP version (<code class="docutils literal notranslate"><span class="pre">HTTP/1.1</span></code>).  To disable automatic
sending of <code class="docutils literal notranslate"><span class="pre">Host:</span></code> or <code class="docutils literal notranslate"><span class="pre">Accept-Encoding:</span></code> headers (for example to accept
additional content encodings), specify <em>skip_host</em> or <em>skip_accept_encoding</em>
with non-False values.</p>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPConnection.putheader">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">putheader</code><span class="sig-paren">(</span><em class="sig-param">header</em>, <em class="sig-param">argument</em><span class="optional">[</span>, <em class="sig-param">...</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.putheader" title="永久链接至目标">¶</a></dt>
<dd><p>Send an <span class="target" id="index-2"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc822.html"><strong>RFC 822</strong></a>-style header to the server.  It sends a line to the server
consisting of the header, a colon and a space, and the first argument.  If more
arguments are given, continuation lines are sent, each consisting of a tab and
an argument.</p>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPConnection.endheaders">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">endheaders</code><span class="sig-paren">(</span><em class="sig-param">message_body=None</em>, <em class="sig-param">*</em>, <em class="sig-param">encode_chunked=False</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.endheaders" title="永久链接至目标">¶</a></dt>
<dd><p>Send a blank line to the server, signalling the end of the headers. The
optional <em>message_body</em> argument can be used to pass a message body
associated with the request.</p>
<p>If <em>encode_chunked</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the result of each iteration of
<em>message_body</em> will be chunk-encoded as specified in <span class="target" id="index-3"></span><a class="rfc reference external" href="https://tools.ietf.org/html/rfc7230.html"><strong>RFC 7230</strong></a>,
Section 3.3.1.  How the data is encoded is dependent on the type of
<em>message_body</em>.  If <em>message_body</em> implements the <a class="reference internal" href="../c-api/buffer.html#bufferobjects"><span class="std std-ref">buffer interface</span></a> the encoding will result in a single chunk.
If <em>message_body</em> is a <a class="reference internal" href="collections.abc.html#collections.abc.Iterable" title="collections.abc.Iterable"><code class="xref py py-class docutils literal notranslate"><span class="pre">collections.abc.Iterable</span></code></a>, each iteration
of <em>message_body</em> will result in a chunk.  If <em>message_body</em> is a
<a class="reference internal" href="../glossary.html#term-file-object"><span class="xref std std-term">file object</span></a>, each call to <code class="docutils literal notranslate"><span class="pre">.read()</span></code> will result in a chunk.
The method automatically signals the end of the chunk-encoded data
immediately after <em>message_body</em>.</p>
<div class="admonition note">
<p class="admonition-title">注解</p>
<p>Due to the chunked encoding specification, empty chunks
yielded by an iterator body will be ignored by the chunk-encoder.
This is to avoid premature termination of the read of the request by
the target server due to malformed encoding.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">3.6 新版功能: </span>Chunked encoding support.  The <em>encode_chunked</em> parameter was
added.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPConnection.send">
<code class="sig-prename descclassname">HTTPConnection.</code><code class="sig-name descname">send</code><span class="sig-paren">(</span><em class="sig-param">data</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPConnection.send" title="永久链接至目标">¶</a></dt>
<dd><p>Send data to the server.  This should be used directly only after the
<a class="reference internal" href="#http.client.HTTPConnection.endheaders" title="http.client.HTTPConnection.endheaders"><code class="xref py py-meth docutils literal notranslate"><span class="pre">endheaders()</span></code></a> method has been called and before <a class="reference internal" href="#http.client.HTTPConnection.getresponse" title="http.client.HTTPConnection.getresponse"><code class="xref py py-meth docutils literal notranslate"><span class="pre">getresponse()</span></code></a> is
called.</p>
</dd></dl>

</div>
<div class="section" id="httpresponse-objects">
<span id="id2"></span><h2>HTTPResponse 对象<a class="headerlink" href="#httpresponse-objects" title="永久链接至标题">¶</a></h2>
<p>An <a class="reference internal" href="#http.client.HTTPResponse" title="http.client.HTTPResponse"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPResponse</span></code></a> instance wraps the HTTP response from the
server.  It provides access to the request headers and the entity
body.  The response is an iterable object and can be used in a with
statement.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">在 3.5 版更改: </span>The <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a> interface is now implemented and
all of its reader operations are supported.</p>
</div>
<dl class="method">
<dt id="http.client.HTTPResponse.read">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">read</code><span class="sig-paren">(</span><span class="optional">[</span><em class="sig-param">amt</em><span class="optional">]</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.read" title="永久链接至目标">¶</a></dt>
<dd><p>Reads and returns the response body, or up to the next <em>amt</em> bytes.</p>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPResponse.readinto">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">readinto</code><span class="sig-paren">(</span><em class="sig-param">b</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.readinto" title="永久链接至目标">¶</a></dt>
<dd><p>Reads up to the next len(b) bytes of the response body into the buffer <em>b</em>.
Returns the number of bytes read.</p>
<div class="versionadded">
<p><span class="versionmodified added">3.3 新版功能.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPResponse.getheader">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">getheader</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">default=None</em><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.getheader" title="永久链接至目标">¶</a></dt>
<dd><p>Return the value of the header <em>name</em>, or <em>default</em> if there is no header
matching <em>name</em>.  If there is more than one  header with the name <em>name</em>,
return all of the values joined by ', '.  If 'default' is any iterable other
than a single string, its elements are similarly returned joined by commas.</p>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPResponse.getheaders">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">getheaders</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.getheaders" title="永久链接至目标">¶</a></dt>
<dd><p>Return a list of (header, value) tuples.</p>
</dd></dl>

<dl class="method">
<dt id="http.client.HTTPResponse.fileno">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">fileno</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.client.HTTPResponse.fileno" title="永久链接至目标">¶</a></dt>
<dd><p>Return the <code class="docutils literal notranslate"><span class="pre">fileno</span></code> of the underlying socket.</p>
</dd></dl>

<dl class="attribute">
<dt id="http.client.HTTPResponse.msg">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">msg</code><a class="headerlink" href="#http.client.HTTPResponse.msg" title="永久链接至目标">¶</a></dt>
<dd><p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPMessage</span></code> instance containing the response
headers.  <code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPMessage</span></code> is a subclass of
<a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">email.message.Message</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="http.client.HTTPResponse.version">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">version</code><a class="headerlink" href="#http.client.HTTPResponse.version" title="永久链接至目标">¶</a></dt>
<dd><p>HTTP protocol version used by server.  10 for HTTP/1.0, 11 for HTTP/1.1.</p>
</dd></dl>

<dl class="attribute">
<dt id="http.client.HTTPResponse.status">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">status</code><a class="headerlink" href="#http.client.HTTPResponse.status" title="永久链接至目标">¶</a></dt>
<dd><p>由服务器返回的状态码。</p>
</dd></dl>

<dl class="attribute">
<dt id="http.client.HTTPResponse.reason">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">reason</code><a class="headerlink" href="#http.client.HTTPResponse.reason" title="永久链接至目标">¶</a></dt>
<dd><p>Reason phrase returned by server.</p>
</dd></dl>

<dl class="attribute">
<dt id="http.client.HTTPResponse.debuglevel">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">debuglevel</code><a class="headerlink" href="#http.client.HTTPResponse.debuglevel" title="永久链接至目标">¶</a></dt>
<dd><p>A debugging hook.  If <a class="reference internal" href="#http.client.HTTPResponse.debuglevel" title="http.client.HTTPResponse.debuglevel"><code class="xref py py-attr docutils literal notranslate"><span class="pre">debuglevel</span></code></a> is greater than zero, messages
will be printed to stdout as the response is read and parsed.</p>
</dd></dl>

<dl class="attribute">
<dt id="http.client.HTTPResponse.closed">
<code class="sig-prename descclassname">HTTPResponse.</code><code class="sig-name descname">closed</code><a class="headerlink" href="#http.client.HTTPResponse.closed" title="永久链接至目标">¶</a></dt>
<dd><p>Is <code class="docutils literal notranslate"><span class="pre">True</span></code> if the stream is closed.</p>
</dd></dl>

</div>
<div class="section" id="examples">
<h2>例子<a class="headerlink" href="#examples" title="永久链接至标题">¶</a></h2>
<p>Here is an example session that uses the <code class="docutils literal notranslate"><span class="pre">GET</span></code> method:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="s2">&quot;www.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;GET&quot;</span><span class="p">,</span> <span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r1</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">r1</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">r1</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">200 OK</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data1</span> <span class="o">=</span> <span class="n">r1</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>  <span class="c1"># This will return entire content.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># The following example demonstrates reading data in chunks.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;GET&quot;</span><span class="p">,</span> <span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r1</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
<span class="gp">... </span>    <span class="n">chunk</span> <span class="o">=</span> <span class="n">r1</span><span class="o">.</span><span class="n">read</span><span class="p">(</span><span class="mi">200</span><span class="p">)</span>  <span class="c1"># 200 bytes</span>
<span class="gp">... </span>    <span class="k">if</span> <span class="ow">not</span> <span class="n">chunk</span><span class="p">:</span>
<span class="gp">... </span>         <span class="k">break</span>
<span class="gp">... </span>    <span class="nb">print</span><span class="p">(</span><span class="nb">repr</span><span class="p">(</span><span class="n">chunk</span><span class="p">))</span>
<span class="go">b&#39;&lt;!doctype html&gt;\n&lt;!--[if&quot;...</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># Example of an invalid request</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="s2">&quot;docs.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;GET&quot;</span><span class="p">,</span> <span class="s2">&quot;/parrot.spam&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">r2</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">r2</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">r2</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">404 Not Found</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data2</span> <span class="o">=</span> <span class="n">r2</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Here is an example session that uses the <code class="docutils literal notranslate"><span class="pre">HEAD</span></code> method.  Note that the
<code class="docutils literal notranslate"><span class="pre">HEAD</span></code> method never returns any data.</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPSConnection</span><span class="p">(</span><span class="s2">&quot;www.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;HEAD&quot;</span><span class="p">,</span> <span class="s2">&quot;/&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">res</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">res</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">200 OK</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="go">0</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">==</span> <span class="sa">b</span><span class="s1">&#39;&#39;</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Here is an example session that shows how to <code class="docutils literal notranslate"><span class="pre">POST</span></code> requests:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span><span class="o">,</span> <span class="nn">urllib.parse</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">params</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">parse</span><span class="o">.</span><span class="n">urlencode</span><span class="p">({</span><span class="s1">&#39;@number&#39;</span><span class="p">:</span> <span class="mi">12524</span><span class="p">,</span> <span class="s1">&#39;@type&#39;</span><span class="p">:</span> <span class="s1">&#39;issue&#39;</span><span class="p">,</span> <span class="s1">&#39;@action&#39;</span><span class="p">:</span> <span class="s1">&#39;show&#39;</span><span class="p">})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">headers</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;Content-type&quot;</span><span class="p">:</span> <span class="s2">&quot;application/x-www-form-urlencoded&quot;</span><span class="p">,</span>
<span class="gp">... </span>           <span class="s2">&quot;Accept&quot;</span><span class="p">:</span> <span class="s2">&quot;text/plain&quot;</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s2">&quot;bugs.python.org&quot;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;POST&quot;</span><span class="p">,</span> <span class="s2">&quot;&quot;</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">headers</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">302 Found</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="n">response</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span>
<span class="go">b&#39;Redirecting to &lt;a href=&quot;http://bugs.python.org/issue12524&quot;&gt;http://bugs.python.org/issue12524&lt;/a&gt;&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Client side <code class="docutils literal notranslate"><span class="pre">HTTP</span> <span class="pre">PUT</span></code> requests are very similar to <code class="docutils literal notranslate"><span class="pre">POST</span></code> requests. The
difference lies only the server side where HTTP server will allow resources to
be created via <code class="docutils literal notranslate"><span class="pre">PUT</span></code> request. It should be noted that custom HTTP methods
are also handled in <a class="reference internal" href="urllib.request.html#urllib.request.Request" title="urllib.request.Request"><code class="xref py py-class docutils literal notranslate"><span class="pre">urllib.request.Request</span></code></a> by setting the appropriate
method attribute. Here is an example session that shows how to send a <code class="docutils literal notranslate"><span class="pre">PUT</span></code>
request using http.client:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="c1"># This creates an HTTP message</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># with the content of BODY as the enclosed representation</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c1"># for the resource http://localhost:8080/file</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">http.client</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">BODY</span> <span class="o">=</span> <span class="s2">&quot;***filecontents***&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">HTTPConnection</span><span class="p">(</span><span class="s2">&quot;localhost&quot;</span><span class="p">,</span> <span class="mi">8080</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">conn</span><span class="o">.</span><span class="n">request</span><span class="p">(</span><span class="s2">&quot;PUT&quot;</span><span class="p">,</span> <span class="s2">&quot;/file&quot;</span><span class="p">,</span> <span class="n">BODY</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">response</span> <span class="o">=</span> <span class="n">conn</span><span class="o">.</span><span class="n">getresponse</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">response</span><span class="o">.</span><span class="n">status</span><span class="p">,</span> <span class="n">response</span><span class="o">.</span><span class="n">reason</span><span class="p">)</span>
<span class="go">200, OK</span>
</pre></div>
</div>
</div>
<div class="section" id="httpmessage-objects">
<span id="id3"></span><h2>HTTPMessage Objects<a class="headerlink" href="#httpmessage-objects" title="永久链接至标题">¶</a></h2>
<p>An <code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPMessage</span></code> instance holds the headers from an HTTP
response.  It is implemented using the <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">email.message.Message</span></code></a> class.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">目录</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code> --- HTTP 协议客户端</a><ul>
<li><a class="reference internal" href="#httpconnection-objects">HTTPConnection 对象</a></li>
<li><a class="reference internal" href="#httpresponse-objects">HTTPResponse 对象</a></li>
<li><a class="reference internal" href="#examples">例子</a></li>
<li><a class="reference internal" href="#httpmessage-objects">HTTPMessage Objects</a></li>
</ul>
</li>
</ul>

  <h4>上一个主题</h4>
  <p class="topless"><a href="http.html"
                        title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http</span></code> --- HTTP 模块</a></p>
  <h4>下一个主题</h4>
  <p class="topless"><a href="ftplib.html"
                        title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ftplib</span></code> --- FTP 协议客户端</a></p>
  <div role="note" aria-label="source link">
    <h3>本页</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">提交 Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.7/Doc/library/http.client.rst"
            rel="nofollow">显示源代码
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="总目录"
             >索引</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python 模块索引"
             >模块</a> |</li>
        <li class="right" >
          <a href="ftplib.html" title="ftplib --- FTP 协议客户端"
             >下一页</a> |</li>
        <li class="right" >
          <a href="http.html" title="http --- HTTP 模块"
             >上一页</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">3.7.8 Documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="internet.html" >互联网协议和支持</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="快速搜索" type="text" name="q" />
          <input type="submit" value="转向" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">版权所有</a> 2001-2020, Python Software Foundation.
    <br />
    Python 软件基金会是一个非盈利组织。
    <a href="https://www.python.org/psf/donations/">请捐助。</a>
    <br />
    最后更新于 6月 29, 2020.
    <a href="../bugs.html">发现了问题</a>？
    <br />
    使用<a href="http://sphinx.pocoo.org/">Sphinx</a>2.3.1 创建。
    </div>

  </body>
</html>